package com.example.haohao.myview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.View;


/**
 * @author fjwu :
 * @discription : 分时最后一个闪烁点
 * create on 2020/2/24
 */

public class MinuteTwinkleView  extends View {

    private float scale = 1;
    private Paint linePaint;
    private int r = DensityUtils.dp2px(6);
    private ValueAnimator scaleAnim;

    private PointF pointF;

    public MinuteTwinkleView(Context context) {
        super(context);
        init();
    }

    public MinuteTwinkleView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MinuteTwinkleView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    /**
     * 初始化
     */
    private void init(){
        pointF=new PointF();
        pointF.x=100;
        pointF.y=100;
        linePaint = new Paint();
        linePaint.setColor(Color.parseColor("#979797"));
        linePaint.setStrokeWidth(3.0f);
        linePaint.setAntiAlias(true);
        if(scaleAnim == null) {
            scaleAnim = ValueAnimator.ofFloat(1, 0.5f, 1);
            scaleAnim.setDuration(2000);
            scaleAnim.setRepeatCount(-1);
            scaleAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    scale = (float) animation.getAnimatedValue();
                        postInvalidate();
                }
            });
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.TRANSPARENT);
        if(pointF != null ) {
                linePaint.setColor(Color.parseColor("#8838A4FF"));
                canvas.drawCircle(pointF.x, pointF.y, r * scale, linePaint);
                linePaint.setColor(Color.parseColor("#38A4FF"));
                canvas.drawCircle(pointF.x, pointF.y, r/2, linePaint);
        }
    }

    /**
     * 开始动画
     */
    private void startDrawAni(){
        if(scaleAnim != null){
            if(!scaleAnim.isStarted()){
                scaleAnim.start();
            }
        }
    }


    /**
     * 结束动画
     */
    private void stopDrawAni(){
        if(scaleAnim != null){
            if(scaleAnim.isStarted()){
                scaleAnim.end();
            }
        }
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startDrawAni();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopDrawAni();
    }


}
